package controller;

import javax.servlet.http.HttpSession;

import model.User;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller

public class NavigateController {

	static  Logger log = Logger.getLogger(CRUDController.class.getName());
	
	public NavigateController() {
		log.info("init Navigate Controller");
	}
	
	@RequestMapping(value = "/home", method = RequestMethod.GET)
	public String navigateHome( ) {
		return "welcome";
	}
	
	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public String navigateRegister( ){
		return "register";
	}

	@RequestMapping(value = "/manufacturer", method = RequestMethod.GET)
	public String navigateManufacturers(  HttpSession session) {
		if (session.getAttribute("userInfo") ==null) {
			return "welcome";
		}
		User usr = (User) session.getAttribute("userInfo");
		int role = usr.getType();
		if(role != 0 && role != 1){
			return "welcome";
		}
		return "default";
	}

	@RequestMapping(value = "/product", method = RequestMethod.GET)
	public String navigateProducts( HttpSession session) {
		if (session.getAttribute("userInfo") ==null) {
			return "welcome";
		}
		User usr = (User) session.getAttribute("userInfo");
		int role = usr.getType();
		if(role != 0 && role != 1){
			return "welcome";
		}
		return "product";
	}

	@RequestMapping(value = "/search", method = RequestMethod.GET)
	public String navigateSearch( HttpSession session) {
		if (session.getAttribute("userInfo") ==null) {
			return "welcome";
		}
		User usr = (User) session.getAttribute("userInfo");
		int role = usr.getType();
		if(role != 0 && role != 1){
			return "welcome";
		}
		return "search";
	}

	@RequestMapping(value = "/review", method = RequestMethod.GET)
	public String navigateReview( HttpSession session) {
		if (session.getAttribute("userInfo") ==null) {
			return "welcome";
		}
		User usr = (User) session.getAttribute("userInfo");
		int role = usr.getType();
		if(role != 0){
			return "welcome";
		}
		return "review";
	}

	@RequestMapping(value = "/about", method = RequestMethod.GET)
	public String navigateAbout( HttpSession session) {
		return "about";
	}
	
}
